package engine.render;

import engine.util.ImageUtil;
import java.awt.image.BufferedImage;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;

public class Texture
{
    
    public static final int TEX_TYPE_COLOR = 0;
    public static final int TEX_TYPE_NORMAL = 1;
    public static final int TEX_TYPE_SHADOW = 2;
    
    
    private String fileName;
    
    private int textureType;
    //<editor-fold defaultstate="collapsed" desc="getType">
    public int getType()
    {
        return this.textureType;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="setType">
    public void setType(final int texType)
    {
        this.textureType = texType;
    }
    //</editor-fold>
    
    private int textureID;
    
    public Texture(String filename)
    {
        this.fileName = filename;
        this.textureType = Texture.TEX_TYPE_COLOR; // Defaults to: TYPE_COLOR
        BufferedImage image = ImageUtil.get().loadImage(this.fileName);
        this.textureID = ImageUtil.get().genGLTexture(image);
    }
    
    public void bind()
    {
        glActiveTexture(GL_TEXTURE0 + this.textureType);
        glBindTexture(GL_TEXTURE_2D, this.textureID);
    }
    
    public void unbind()
    {
        glActiveTexture(GL_TEXTURE0 + this.textureType);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
    
    public String toString()
    {
        String s = "";
        s += "TEXTURE COMPONENT:" + "\n";
        s += "   FileName: " + this.fileName + "\n";
        s += "   ID: " + this.textureID + "\n";
        s += "   Type: " + this.textureType + "\n";
        return s;
    }
    
    public void destroy()
    {
        glDeleteTextures(this.textureID);
        this.fileName = null;
        this.textureID = 0;
    }
    
}
